package com.geqian.web.generator;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * mysql、jdbc、java三者数据类型映射关系
 * @author geqian
 * @date 23:30 2023/11/18
 */
public class TypeConverter {

    private static final Map<String, Map.Entry<String, String>> typeMapping = new HashMap<>();

    static {
        //布尔类型
        typeMapping.put("bit", new AbstractMap.SimpleEntry<>("BIT", "Boolean"));
        typeMapping.put("boolean", new AbstractMap.SimpleEntry<>("BIT", "Boolean"));
        typeMapping.put("tinyint", new AbstractMap.SimpleEntry<>("TINYINT", "Boolean"));
        typeMapping.put("bool", new AbstractMap.SimpleEntry<>("BIT", "Boolean"));
        //数值类型
        typeMapping.put("smallint", new AbstractMap.SimpleEntry<>("SMALLINT", "Short"));
        typeMapping.put("mediumint", new AbstractMap.SimpleEntry<>("INTEGER", "Integer"));
        typeMapping.put("int", new AbstractMap.SimpleEntry<>("INTEGER", "Integer"));
        typeMapping.put("integer", new AbstractMap.SimpleEntry<>("INTEGER", "Integer"));
        typeMapping.put("bigint", new AbstractMap.SimpleEntry<>("BIGINT", "Long"));
        typeMapping.put("float", new AbstractMap.SimpleEntry<>("FLOAT", "Float"));
        typeMapping.put("double", new AbstractMap.SimpleEntry<>("DOUBLE", "Double"));
        typeMapping.put("decimal", new AbstractMap.SimpleEntry<>("DECIMAL", "BigDecimal"));
        typeMapping.put("numeric", new AbstractMap.SimpleEntry<>("NUMERIC", "BigDecimal"));

        //字符串类型
        typeMapping.put("varchar", new AbstractMap.SimpleEntry<>("VARCHAR", "String"));
        typeMapping.put("char", new AbstractMap.SimpleEntry<>("CHAR", "String"));
        typeMapping.put("text", new AbstractMap.SimpleEntry<>("VARCHAR", "String"));
        typeMapping.put("longtext", new AbstractMap.SimpleEntry<>("VARCHAR", "String"));
        typeMapping.put("tinytext", new AbstractMap.SimpleEntry<>("VARCHAR", "String"));
        typeMapping.put("mediumtext", new AbstractMap.SimpleEntry<>("VARCHAR", "String"));
        typeMapping.put("enum", new AbstractMap.SimpleEntry<>("CHAR", "String"));
        typeMapping.put("set", new AbstractMap.SimpleEntry<>("CHAR", "String"));

        //时间日期类型
        typeMapping.put("date", new AbstractMap.SimpleEntry<>("DATE", "Date"));
        typeMapping.put("time", new AbstractMap.SimpleEntry<>("TIME", "Time"));
        typeMapping.put("timestamp", new AbstractMap.SimpleEntry<>("TIMESTAMP", "Date"));
        typeMapping.put("datetime", new AbstractMap.SimpleEntry<>("DATE", "Date"));
        typeMapping.put("year", new AbstractMap.SimpleEntry<>("YEAR", "Date"));

        //字节类型
        typeMapping.put("varbinary", new AbstractMap.SimpleEntry<>("VARBINARY", "byte[]"));
        typeMapping.put("blob", new AbstractMap.SimpleEntry<>("BLOB", "byte[]"));
        typeMapping.put("binary", new AbstractMap.SimpleEntry<>("BINARY", "byte[]"));
        typeMapping.put("tinyblob", new AbstractMap.SimpleEntry<>("TINYBLOB", "byte[]"));
        typeMapping.put("mediumblob", new AbstractMap.SimpleEntry<>("MEDIUMBLOB", "byte[]"));
        typeMapping.put("longblob", new AbstractMap.SimpleEntry<>("LONGBLOB", "byte[]"));

    }


    public static String getJdbcType(String columType) {
        return typeMapping.get(columType.toLowerCase(Locale.ROOT)).getKey();
    }


    public static String getJavaType(String columType) {
        return typeMapping.get(columType.toLowerCase(Locale.ROOT)).getValue();
    }

}
